package Veterinaria.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import Veterinaria.excepcion.CodigoDuplicadoExcepcion;
import Veterinaria.excepcion.DAOExcepcion;
import Veterinaria.modelo.Doctor;
import Veterinaria.negocio.GestionPersona;
/**
 * Servlet implementation class DoctorServlet
 */
@WebServlet("/DoctorServlet")
public class DoctorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoctorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		System.out.println("Entro");
		PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");
        
        System.out.println("Servlets");
        JSONArray arrayObj = new JSONArray();
        JSONObject obj1 = new JSONObject();
        System.out.println("Servlets1");
        	
		GestionPersona doctor = new GestionPersona();
		String nom = null;
		try {
			System.out.println("Servlets2");
			String query = request.getParameter("term");
	        System.out.println(query);
	        query = query.toLowerCase();
	        
			Collection<Doctor> doctores = doctor.ListaDoctorXNombre(query);

			for (Doctor doc : doctores) {
				if (doc.getApellido_persona().toLowerCase().startsWith(query))
					nom = doc.getApellido_persona() + " " + doc.getNombre_persona();
				obj1.put("label", nom);
				obj1.put("value", nom);
				obj1.put("id", doc.getCodigo_persona());
				obj1.put("nom", doc.getNombre_persona());
				obj1.put("ape", doc.getApellido_persona());
				obj1.put("email", doc.getEmail_persona());
				obj1.put("telf", doc.getTelefono_fijo());
				obj1.put("cel", doc.getTelefono_celular());
				obj1.put("sex", doc.getSexo());
				obj1.put("ruc", doc.getRuc());
				obj1.put("usu", doc.getUsuario_sistema());
				obj1.put("pass", doc.getPassword());				
				arrayObj.add(obj1.toString());
				obj1.clear();
			}

		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        out.println(arrayObj);
	        out.close();
		}		
}
	
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
			GestionPersona per = new GestionPersona();
			
			String x = request.getParameter("txtcondicion");
			String  nombre = request.getParameter("NomDueno");
			String  apellido = request.getParameter("apellido");
			String  tipopersona = request.getParameter("tipopersona");
			String  email = request.getParameter("email");
			String  direccion = request.getParameter("direccion");
			String  telefono = request.getParameter("telefono");
			String  celular = request.getParameter("celular");
			String  sexodueno = request.getParameter("sexodueno");
			String  usuario = request.getParameter("usuario");
			String  password = request.getParameter("password");
			String  txtIddoctor = request.getParameter("txtIddoctor");

	   if (x.equals("INSERTAR")){		

			Doctor codigo = null;
			try {
				codigo = per.GenerarCodigoDoctor();
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
			try {
				per.insertaDoctor(codigo.getCodigo_persona(), nombre, apellido, tipopersona, email, direccion, telefono, celular, "", sexodueno, "1", codigo.getCodigo_persona(), usuario, password);
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (CodigoDuplicadoExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			RequestDispatcher  rd= request.getRequestDispatcher("doctor.jsp");
			rd.forward(request, response);

		}else if (x.equals("MODIFICAR")){

		
			try {
				per.actualizarDoctor(txtIddoctor, nombre, apellido, tipopersona, email, direccion, telefono, celular, "", sexodueno, "1", txtIddoctor, usuario, password);

			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			RequestDispatcher  rd= request.getRequestDispatcher("modificar_doctor.jsp");
			rd.forward(request, response);
		}else{

			try {
				per.EliminarDoctor(txtIddoctor);

			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			RequestDispatcher  rd= request.getRequestDispatcher("eliminar_doctor.jsp");
			rd.forward(request, response);
			
		}
	   
		
	}

}
